package org.footballmanager.pandafootball.system.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.system.domain.FootMission;
import org.footballmanager.pandafootball.system.domain.FootMissionHistory;
import org.footballmanager.pandafootball.system.form.impl.FootMissionSearchForm;
import org.footballmanager.pandafootball.system.service.FootMissionHistoryService;
import org.footballmanager.pandafootball.system.service.FootMissionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Service
public class FootMissionHistoryServiceImpl extends ServiceImpl<FootMissionHistory> implements FootMissionHistoryService {
    @Autowired
    private FootMissionService footMissionService;

    /**
     * 处理历史记录（定时任务）
     *
     * @param request 请求
     */
    @Override
    public void processHistoryRecord(HttpServletRequest request) {
        List<FootMission> missionList = footMissionService.getall();
        if (missionList != null && !missionList.isEmpty()) {
            for (FootMission mission : missionList) {
                if (!mission.getComplete()) {
                    mission.setComplete(true);
                    footMissionService.updateNotNull(mission, null);
                }
                FootMissionHistory history = new FootMissionHistory();
                BeanUtils.copyProperties(history, mission);
                history.setId(null);
                this.save(history, null);
                footMissionService.deleteNotLogic(mission.getId());
            }
        }
    }

    /**
     * 查询
     *
     * @param footMissionSearchForm 条件
     * @return 列表
     */
    @Override
    public List<FootMissionHistory> findall(FootMissionSearchForm footMissionSearchForm) {
        return this.findall(footMissionSearchForm.buildSearchCommond());
    }
}
